/*
License:
this file is part of sflu - Simple Flex Logging UI
* Copyright (c) 2008, azbest(azbest.pro@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY azbest(azbest.pro@gmail.com) ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL azbest(azbest.pro@gmail.com) BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


package pl.vdl.azbest.sflu;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/** List file choosen by user. */
public class LogLister {
	private static LogLister instance = null;

	private LogLister() {
	}

	public static LogLister getInstance() {
		if (instance == null) {
			instance = new LogLister();
			return instance;
		} else {
			return instance;
		}
	}

	public static void main(String[] args) {

		final String help = "Usage: \n"
				+ "lister <filename_with_text_tocheck> \n"
				+ "lister <filename_with_text_tocheck> -c <check_every_sec> [ms]\n"
				+ "Examples:\n"
				+ ">lister : will look for flashlog in \n"
				+ " C:\\Documents and Settings\\USERDIR\\Dane aplikacji\\"
				+ " Macromedia\\Flash Player\\Logs\\flashlog.txt\n"
				+ ">lister flashlog.txt : will list inhalt of \n"
				+ " flashlog file and check for changes every 0.5 sec\n"
				+ ">lister flashlog.txt -c 100 : will list inhalt of \n"
				+ " flashlog file and check for changes every 0.1 sec (100 ms)\n"
				+ ">flashlog -c 200 : open default and check every .2 sec";

		System.err.println(help);

		String tmpPath = "";
		int tmpPeriod = 500;
		switch (args.length) {
		case 0: {
			String userdir = System.getProperty("user.home");
			tmpPath = userdir
					+ "\\Dane aplikacji\\Macromedia\\Flash Player\\Logs\\flashlog.txt";
			break;
		}

		case 1: {
			tmpPath = args[0];
			break;
		}

		case 2: {
			if (args[0].equals("-c")) {
				try {
					tmpPeriod = Integer.parseInt(args[1]);
				} catch (NumberFormatException e) {
					System.err
							.println("lease provide VALID number for miliseconds. Ex: 200 ");
					System.exit(1);
				}
				String userdir = System.getProperty("user.home");
				tmpPath = userdir
						+ "\\Dane aplikacji\\Macromedia\\Flash Player\\Logs\\flashlog.txt";

			} else {
				System.err.println(help);
				System.exit(1);
			}
			break;
		}
		case 3: {
			tmpPath = args[0];
			if (args[1].equals("-c")) {
				try {
					tmpPeriod = Integer.parseInt(args[2]);
				} catch (NumberFormatException e) {
					System.err
							.println("lease provide VALID number for miliseconds. Ex: 200 ");
					System.exit(1);
				}
			} else {
				System.err.println(help);
				System.exit(1);
			}
			break;
		}
		default:
			System.err.println(help);
			break;
		}

		if (!new File(tmpPath).exists()) {
			System.out
					.println("Can't open requested file for listing ! - File not found\n"
							+ "Enter full path to file");
			System.err.println(help);
		}

		final String filePath = tmpPath;
		final int period = tmpPeriod;
		System.out.println("Path " + tmpPath);
		System.out.println("Period " + tmpPeriod);
		System.out.println("Args nu " + args.length);

		System.err.println("Raeding .....");
		class CheckInhalt implements Runnable {

			public void run() {
				BufferedReader br = null;
				File file = null;
				FileReader freader = null;

				try {
					file = new File(filePath);
					freader = new FileReader(file);
					br = new BufferedReader(freader);
					// file reading
					String line = null;

					boolean firstimeread = true;
					long startByte = 0;
					while (true) {



						// eendless loop
						if (!firstimeread)
							startByte = file.length();

						br.skip(startByte);
						while ((line = br.readLine()) != null) {
							System.err.println(line);
						}
						try {
							Thread.sleep(period);
						} catch (InterruptedException e) {
							// TODO: handle exception
						}
					}

				} catch (FileNotFoundException e) {
					System.out
							.println("Can't open requested file for listing ! - File not found\n"
									+ "Enter full path to file");
					System.err.println(help);
				} catch (IOException e) {
					System.out.println("Can't read from requested file  \n");
				} finally {
					try {
						br.close();
					} catch (IOException e) {
						System.err.println("IOEx as closing stream\n\n");
						e.printStackTrace();
					}

				}
			}
		}// chacek inhalt
		CheckInhalt ci = new CheckInhalt();
		Thread t = new Thread(ci);
		t.start();

	}
}